package homework;

/**
 * 设计一个BankAccount类，包含账户号(accountNumber)、账户名(accountName)和账户余额(balance)等属性
 * 。实现以下方法：
 * deposit(double amount)：存款，将金额加到余额上。
 * withdraw(double amount)：取款，从余额中减去金额。如果金额大于余额，应抛出异常。
 * getBalance()：获取账户余额。（下边贴代码截图和运行结果）
 */
public class BankAccount {
    public static void main(String[] args) {
        BankAccount account = new BankAccount("1234567890",
                "张三",
                10000);
        //存款 1000
        account.deposit(1000);
        System.out.println(account.getBalance());
        //取款
        try {
            account.withdraw(10000);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        System.out.println(account.getBalance());
        try{
            account.withdraw(2000);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        System.out.println(account.getBalance());

    }
    private String accountNumber;
    private String accountName;
    private double balance;
    public void deposit(double money){
        this.balance+=money;
        System.out.println("存款成功");
    }
    public void withdraw(double money) throws Exception{
        if(this.balance<money){
            //余额不足
            throw new Exception("取款失败，余额不足");
        }else{
            this.balance = this.balance-money;
            System.out.println("取款成功");
        }
    }
    public double getBalance(){
        return this.balance;
    }
    public BankAccount(String accountNumber,
                       String accountName,
                       double balance) {
        this.accountNumber = accountNumber;
        this.accountName = accountName;
        this.balance = balance;
    }
}
